//系统商品规则
import { useState, useEffect } from 'react';
import { Switch, message } from 'antd';
import CommonLoading from '@/components/CommonLoading';
import { isSyncSysGoods, setIsSyncSysGoods } from '../service';
import styles from './index.module.less';

//商品规则
const SysGoodsRule = () => {
  const [loading, setLoading] = useState(false);
  const [checked, setChecked] = useState(false);

  useEffect(() => {
    getStatusWhenInit();
  }, []);

  //同步商品是否自动生成系统商品
  const getStatusWhenInit = async () => {
    setLoading(true);
    const { data = false, success } = await isSyncSysGoods();
    if (success) {
      setChecked(data);
    }
    setLoading(false);
  };

  //切换同步商品是否自动生成系统商品
  const handleSwitch = async (e) => {
    setLoading(true);
    const { success, msg } = await setIsSyncSysGoods({ type: e ? 1 : 0 });
    if (success) {
      message.success(msg);
      getStatusWhenInit();
    }
    setLoading(false);
  };

  return (
    <div style={{ marginTop: '20px' }}>
      <div className={styles.title}>1. 系统商品规则</div>
      <div style={{ display: 'flex', alignItems: 'center' }}>
        <span>同步商品是否自动生成系统商品：</span>
        <Switch
          checkedChildren="是"
          unCheckedChildren="否"
          checked={checked}
          style={{ margin: '6px' }}
          onClick={(e) => handleSwitch(e)}
          size="default"
        />
      </div>
      <CommonLoading loading={loading} />
    </div>
  );
};

export default SysGoodsRule;
